package com.pearadmin.hospital.controller;

import com.github.pagehelper.PageInfo;
import com.pearadmin.hospital.domain.HospitalReturngoods;
import com.pearadmin.common.tools.string.Convert;
import com.pearadmin.common.web.base.BaseController;
import com.pearadmin.common.web.domain.request.PageDomain;
import com.pearadmin.common.web.domain.response.Result;
import com.pearadmin.common.web.domain.response.module.ResultTable;
import com.pearadmin.common.tools.secure.SecurityUtil;
import com.pearadmin.system.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import com.pearadmin.hospital.service.IHospitalReturngoodsService;

import java.time.LocalDateTime;

/**
 * 收到退货Controller
 *
 * @author yuan
 * @date 2021-07-29
 */
@RestController
@RequestMapping("/hospital/returngoods")
public class HospitalReturngoodsController extends BaseController
{
    private String prefix = "hospital/returngoods";

    @Autowired
    private IHospitalReturngoodsService hospitalReturngoodsService;

    @GetMapping("/main")
    @PreAuthorize("hasPermission('/hospital/returngoods/main','hospital:returngoods:main')")
    public ModelAndView main()
    {
        return jumpPage(prefix + "/main");
    }

    /**
     * 查询收到退货列表
     */
    @ResponseBody
    @GetMapping("/data")
    @PreAuthorize("hasPermission('/hospital/returngoods/data','hospital:returngoods:data')")
    public ResultTable list(@ModelAttribute HospitalReturngoods hospitalReturngoods, PageDomain pageDomain)
    {
        PageInfo<HospitalReturngoods> pageInfo = hospitalReturngoodsService.selectHospitalReturngoodsPage(hospitalReturngoods,pageDomain);
        return pageTable(pageInfo.getList(),pageInfo.getTotal());
    }

    /**
     * 新增收到退货
     */
    @GetMapping("/add")
    @PreAuthorize("hasPermission('/hospital/returngoods/add','hospital:returngoods:add')")
    public ModelAndView add()
    {
        return jumpPage(prefix + "/add");
    }

    /**
     * 新增保存收到退货
     */
    @ResponseBody
    @PostMapping("/save")
    @PreAuthorize("hasPermission('/hospital/returngoods/add','hospital:returngoods:add')")
    public Result save(@RequestBody HospitalReturngoods hospitalReturngoods)
    {
        SysUser sysUser = (SysUser)SecurityUtil.currentUserObj();
        hospitalReturngoods.setCreateTime(LocalDateTime.now());
        hospitalReturngoods.setCreateBy(sysUser.getUserId());
        hospitalReturngoods.setCreateName(sysUser.getUsername());
        return decide(hospitalReturngoodsService.insertHospitalReturngoods(hospitalReturngoods));
    }

    /**
     * 修改收到退货
     */
    @GetMapping("/edit")
    @PreAuthorize("hasPermission('/hospital/returngoods/edit','hospital:returngoods:edit')")
    public ModelAndView edit(Integer id, ModelMap mmap)
    {
        HospitalReturngoods hospitalReturngoods = hospitalReturngoodsService.selectHospitalReturngoodsById(id);
        mmap.put("hospitalReturngoods", hospitalReturngoods);
        return jumpPage(prefix + "/edit");
    }

    /**
     * 修改保存收到退货
     */
    @ResponseBody
    @PutMapping("/update")
    @PreAuthorize("hasPermission('/hospital/returngoods/edit','hospital:returngoods:edit')")
    public Result update(@RequestBody HospitalReturngoods hospitalReturngoods)
    {
        SysUser sysUser = (SysUser)SecurityUtil.currentUserObj();
        hospitalReturngoods.setUpdateTime(LocalDateTime.now());
        hospitalReturngoods.setUpdateBy(sysUser.getUserId());
        hospitalReturngoods.setUpdateName(sysUser.getUsername());
        return decide(hospitalReturngoodsService.updateHospitalReturngoods(hospitalReturngoods));
    }

    /**
     * 删除收到退货
     */
    @ResponseBody
    @DeleteMapping( "/batchRemove")
    @PreAuthorize("hasPermission('/hospital/returngoods/remove','hospital:returngoods:remove')")
    public Result batchRemove(String ids)
    {
        return decide(hospitalReturngoodsService.deleteHospitalReturngoodsByIds(Convert.toStrArray(ids)));
    }

    /**
     * 删除
     */
    @ResponseBody
    @DeleteMapping("/remove/{id}")
    @PreAuthorize("hasPermission('/hospital/returngoods/remove','hospital:returngoods:remove')")
    public Result remove(@PathVariable("id") Integer id)
    {
        return decide(hospitalReturngoodsService.deleteHospitalReturngoodsById(id));
    }
}
